package cn.shanke.torrentmanagerserver.util;

import org.springframework.util.StringUtils;

import java.net.URI;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

public class StringUtil {

    public static String join(List<? extends Object> objs, String joinChar) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < objs.size(); i++) {
            sb.append(joinChar).append(objs.get(i));
        }
        return sb.toString().replaceFirst(",", "");
    }

    public static String[] splitToArray(String str, String splitChar) {
        if (StringUtils.hasText(str)) return new String[0];
        return str.split(splitChar);
    }

    public static int[] splitToIntArray(String str, String splitChar) {
        if (StringUtils.hasText(str)) return new int[0];
        return Arrays.stream(str.split(splitChar)).mapToInt(value -> Integer.parseInt(value)).toArray();
    }

    public static List<Long> arrayToLongList(String[] array) {
        if (array==null||array.length==0) return Collections.EMPTY_LIST;
        return Arrays.stream(array).map(value -> Long.valueOf(value)).collect(Collectors.toList());
    }

    public static List<Integer> splitToIntegerList(String str, String splitChar) {
        if (StringUtils.hasText(str)) return Collections.EMPTY_LIST;
        return Arrays.stream(str.split(splitChar)).map(value -> Integer.valueOf(value)).collect(Collectors.toList());
    }

    public static List<Long> stringListToLongList(List<String> list) {
        if (list == null || list.isEmpty()) return Collections.EMPTY_LIST;
        return list.stream().map(s->Long.valueOf(s)).collect(Collectors.toList());
    }

    public static String getDomainInUrl(String url) {
        String r =  url.substring(url.indexOf("://") + 3);
        r = r.indexOf("/") > 0?r.substring(0, r.indexOf("/")):r;
        r = r.indexOf(":") > 0?r.split(":")[0]:r;
        String[] temp = r.split("\\.");
        if (temp.length == 3) r = temp[1] + "." + temp[2];
        return r;
    }

    public static String removeLastSlash(String src) {
//        System.out.println(src + " >>>");
        String rst = src;
        if (!StringUtils.hasText(src)) {
            if (src.endsWith("\\\\")) {
                rst = src.substring(0, src.length()-2);
            } else if (src.endsWith("\\")) {
                rst = src.substring(0, src.length()-1);
            } else if (src.endsWith("/")) {
                rst = src.substring(0, src.length()-1);
            }
        }
        return rst;
    }

    public static String removeFirstSlash(String src) {
//        System.out.println(src + " >>>");
        String rst = src;
        if (!StringUtils.hasText(src)) {
            if (src.startsWith("\\\\")) {
                rst = src.substring(2);
            } else if (src.startsWith("\\")) {
                rst = src.substring(1);
            } else if (src.startsWith("/")) {
                rst = src.substring(1);
            }
        }
        return rst;
    }

    public static String removeSidesSlash(String src) {
        return removeFirstSlash(removeLastSlash(src));
    }

//    public static void main(String[] args) {
////        String url = "https://pt.btschool.club:443/getaaa";
////        System.out.println(url);
////        System.out.println(getDomainInUrl(url));
//
//
////        System.out.println(removeLastSlash("/manange"));
////        System.out.println(removeLastSlash("N:"));
////        System.out.println(removeLastSlash("N:\\\\"));
////        System.out.println(removeLastSlash("N:\\"));
//
////        System.out.println(removeFirstSlash("/manange"));
////        System.out.println(removeFirstSlash("N:"));
////        System.out.println(removeFirstSlash("N:\\\\"));
////        System.out.println(removeFirstSlash("N:\\"))
////        ;
//        System.out.println(removeSidesSlash("/manange/"));
//        System.out.println(removeSidesSlash("N:"));
//        System.out.println(removeSidesSlash("N:\\\\"));
//        System.out.println(removeSidesSlash("N:\\"));
//    }
}
